var backofficeMDetail = {
	isEdit : false,
	load : function () {
		var qs =  new QueryString();
		var account =qs.get("account"); //"koko0809@msn.com";//qs.get("customAccount");
		
		Ext.getDom("address").value="";
		Ext.getDom("password").value="";
		
		if (account != null) {
			backofficeMDetail.isEdit = true;
			Ext.getDom("userForm").action = "/ad/sys/com/control/UserService.php?action=managerUpdate";
			Ext.Ajax.request({
				url : "/ad/sys/com/control/UserService.php?action=getUserNotStatus&account="+account,			
				callback  : function (op, su, rsp) {
					var user =  rsp.responseText.toJSON();
					
					Ext.getDom("account").value = user.Account;
					Ext.getDom("name").value = user.Name;
					Ext.getDom("phone").value = user.Phone;
					Ext.getDom("companyName").value = user.CompanyName;
					
					Ext.getDom("address").value = user.Address;
					Ext.getDom("password").value = user.Password;
					Ext.getDom("againPassword").value = user.Password
					
					if ( user.logo != "" &&  user.logo != null) {
						Ext.getDom("preUpLoadImg").src = user.logo;
						Ext.getDom("logo").value = user.logo;
					}
					if (user.Type ==1) {
						document.getElementsByName("type")[0].checked = true;
					} else if (user.Type ==2) {
						document.getElementsByName("type")[1].checked = true;
					}
					
					if (user.ProxyAccount) {
						document.getElementsByName("agency")[0].checked = true;
					} else {
						document.getElementsByName("agency")[1].checked = true;
					}
					
					$('#addr_1').twzipcode({
						countyName: "county",
						districtName: "area",
						zipcodeName: "zipcode",
						countySel: user.County, 
     					districtSel: user.Area,  
     					zipcodeSel : user.Zipcode
					});
				    
					Ext.getDom("count").value = user.Count;
					
					Ext.get("nowDate").dom.innerHTML = user.CreateTime.toDateString();
					
					Ext.Ajax.request({
						url : "/ad/sys/com/control/UserService.php?action=getTargetAccount",
						callback  : function (op, su, rsp) {
							var obj =  rsp.responseText.toJSON();
							addOptions("tagetAccount", obj.result, "account", "companyName");
							setOptions("tagetAccount", user.ProxyAccount);
						}
					}); 	
					
				}
			});
		} else {
			$('#addr_1').twzipcode({
				countyName: "county",
				districtName: "area",
				zipcodeName: "zipcode"
			});
			 	
			Ext.Ajax.request({
				url : "/ad/sys/com/control/UserService.php?action=getTargetAccount",
				callback  : function (op, su, rsp) {
					var obj =  rsp.responseText.toJSON();
					addOptions("tagetAccount", obj.result, "account", "companyName");
				}
			}); 
			Ext.get("nowDate").dom.innerHTML = new Date().string();
		}
		
		
		Ext.get("welcome").dom.innerHTML = getUser().Name;
		this.eventRegister();
	},
	 
	eventRegister : function () {
		Ext.get("confirm").on('click',this.confrim);  //確認,新增使用者		
		Ext.get("btnCheckAccount").on("click",this.checkAccount); //檢查使用者帳號
		Ext.get("btnLogo").on("click",this.upload); 
	},
	 
	upload : function () {
		 var result = null;
		    
		if (getBrowser().isIE) {
		 	result = uploadImg('logo', 30 , 390);
		} else if (getBrowser().isFF) {
			result = uploadImg('logo', 115 , 525);
		} else {
		 	result = uploadImg('logo', 110 , 520);
		}
		if (result != null) {
			Ext.getDom("preUpLoadImg").src = result;
			Ext.getDom("logo").value = result;
		}
	},
	
	checkAccount: function (){
		var account = Ext.get("account").getValue();
		
		if (account == "") {
			alert("請輸入[帳號]!");
			return;
		}
		
		if (!account.isEmail()) {
			alert("帳號非Email格式!");
			return;
		}
	 	 
		Ext.get("loading").show();
		 
		Ext.Ajax.request({
			url : "/ad/sys/com/control/UserService.php?action=checkAccount&account="+account,
			callback : function (options, success, rsp) {
				Ext.get("loading").hide();
				var obj =  rsp.responseText.toJSON();  
				 
				if (obj.result > 0) {
					alert("帳號已被註冊!");
				}  else {
					alert("帳號未被註冊!");
				}
			}
		});
		 
	},
	
	confrim : function () {	   	 
	    
		if (!Ext.get("account").getValue()){
	    	alert("請輸入[帳號]!");
	    	Ext.get("account").focus();
	    	return;
	    }
		
		if (!Ext.get("account").getValue().isEmail()){
	    	alert("[帳號]非正確Email格式!");
	    	Ext.get("account").focus();
	    	return;
	    }
	    
	    if (!Ext.get("name").getValue()) {
	    	alert("請輸入[姓名]!");
	    	Ext.get("name").focus();
	    	return;
	    }
	   
	    if (!Ext.get("phone").getValue().isPhone()) {
	    	alert("[電話]非正確電話格式!");
	    	Ext.get("phone").focus();
	    	return;
	    }  
	     
	    if (!Ext.get("companyName").getValue()) {
	    	alert("請輸入[公司名稱]!");
	    	Ext.get("companyName").focus();
	    	return;
	    }  
	     
	   
	    var county =  getSelectValue(document.getElementsByName("county")[0].options);
	    
	     
	    if (!county) {
	    	alert("請選擇[縣市]!");
	    	document.getElementsByName("county")[0].focus();
	    	return ;
	    }
	    
	    var area = getSelectValue(document.getElementsByName("area")[0].options);
	    var zipcode = document.getElementsByName("zipcode")[0].value;
	  	
	    
	    if (!Ext.get("address").getValue()) {
	    	alert("請輸入[住址]!")
	    	Ext.get("address").focus();
	    	return;
	    }
	    
	    var password = Ext.get("password").getValue();
	    var againPassword = Ext.get("againPassword").getValue();
	    
	    if (!password) {
	    	alert("請輸入[密碼]!");
	    	Ext.get("password").focus();
	    	return ;
	    }
	    
	    if (password != againPassword) {
	    	alert("[密碼]輸入不一致");
	    	Ext.get("againPassword").focus();
	    	return;
	    }
	    
	    if (isNaN(Ext.get("count").getValue())) {
	    	alert("[點數]請輸入數字!");
	    	Ext.get("count").focus();
	    	return;
	    }
	    
	    
	   if (backofficeMDetail.isEdit) {
			waitShow("更新中...");
			
		    Ext.Ajax.request({
		    	method : "POST",
		     	form : "userForm",
		    	callback : function (options, se, rsp) {
		    		waitHide();
		    		 
		    		var obj =  rsp.responseText.toJSON();  
		    		 
		    		if (obj.result =="1") {
		    			alert("更新成功!");
		    			 
		    		} else{
		    			alert("更新失敗!");
		    		}
		    	}
		    	
		    })	   
	   } else {
		    waitShow("新增中...");
		    Ext.Ajax.request({
		    	method : "POST",
		     	form : "userForm",
		    	callback : function (options, se, rsp) {
		    		waitHide();
		    		 
		    		var obj =  rsp.responseText.toJSON();  
		    		 
		    		if (obj.result =="1") {
		    			alert("新增成功!");
		    			location.href="/ad/html/backoffice/backofficePermission.php";
		    		} else{
		    			alert(obj.msg);
		    		}
		    	}
		    	
		    })
	   }
 	}  
}

Ext.onReady(function (){
	backofficeMDetail.load();
});
